﻿using IPR_StudyMeetingMrgSys.Enums;
using IPR_StudyMeetingMrgSys.Extension;
using IPR_StudyMeetingMrgSys.Models;
using log4net;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Reflection;
using System.Web.Http;

namespace IPR_StudyMeetingMrgSys.Controllers
{
    /// <summary>
    /// 试卷类型控制器
    /// </summary>
    public class ExamPaperTypeController : ApiController
    {
        ILog log = log4net.LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
        /// <summary>
        /// 根据ID获取试卷类型信息
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public IHttpActionResult Get(int id)
        {
            ResultMsg resultMsg = new ResultMsg();
            resultMsg.StatusCode = (int)StatusCodeEnum.Success;
            try
            {
                Model.ExamPaperType model = new BLL.ExamPaperType().GetModel(id);
                if (model != null)
                {
                    resultMsg.Info = StatusCodeEnum.Success.GetEnumText();
                    resultMsg.Data = model;
                    resultMsg.Count = 1;
                }
                else
                {
                    resultMsg.Info = "未获取到相应数据";
                }
            }
            catch (Exception ex)
            {
                resultMsg.StatusCode = (int)StatusCodeEnum.Error;
                resultMsg.Info = StatusCodeEnum.Error.GetEnumText();
                resultMsg.Data = ex.Message;
                log.Error(ex);
            }
            return Json(resultMsg);
        }
        /// <summary>
        /// 获取试卷类型列表，用于添加编辑试卷时选择使用
        /// </summary>
        /// <returns></returns>
        [Route("api/ExamPaperType/GetList")]
        [HttpGet]
        public IHttpActionResult GetList()
        {
            ResultMsg resultMsg = new ResultMsg();
            resultMsg.StatusCode = (int)StatusCodeEnum.Success;
            try
            {
                DataSet ds = new BLL.ExamPaperType().GetList(string.Empty);
                resultMsg.Info = StatusCodeEnum.Success.GetEnumText();
                resultMsg.Data = ds.Tables[0];
                resultMsg.Count = ds.Tables[0].Rows.Count;
            }
            catch (Exception ex)
            {
                resultMsg.StatusCode = (int)StatusCodeEnum.Error;
                resultMsg.Info = StatusCodeEnum.Error.GetEnumText();
                resultMsg.Data = ex.Message;
                log.Error(ex);
            }
            return Json(resultMsg);
        }

        /// <summary>
        /// 试卷类型的增加
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [Route("api/ExamPaperType/Add")]
        [HttpPost]
        public IHttpActionResult Add(Model.ExamPaperType model)
        {
            log.Info(JsonConvert.SerializeObject(model));

            ResultMsg resultMsg = new ResultMsg();
            resultMsg.StatusCode = (int)StatusCodeEnum.Success;
            try
            {
                BLL.ExamPaperType _ExamPaperType = new BLL.ExamPaperType();
                if (!_ExamPaperType.Exists(model.EPTName))
                {
                    if (new BLL.ExamPaperType().Add(model) > 0)
                    {
                        resultMsg.Info = "新增成功";
                        resultMsg.Count = 1;
                    }
                    else
                    {
                        resultMsg.Info = "新增失败";
                    }
                }
                else
                {
                    resultMsg.Info = "类型名称已存在";
                }
            }
            catch (Exception ex)
            {
                resultMsg.StatusCode = (int)StatusCodeEnum.Error;
                resultMsg.Info = StatusCodeEnum.Error.GetEnumText();
                resultMsg.Data = ex.Message;
                log.Error(ex);
            }
            return Json(resultMsg);
        }

        /// <summary>
        /// 试卷类型查询
        /// </summary>
        /// <param name="swhere">查询条件</param>
        /// <param name="page">页索引</param>
        /// <param name="limit">页大小</param>
        /// <returns></returns>
        [HttpGet]
        [Route("api/ExamPaperType/Search")]
        public IHttpActionResult Search(string swhere, int page, int limit)
        {
            ResultMsg resultMsg = new ResultMsg();
            resultMsg.StatusCode = (int)StatusCodeEnum.Success;
            try
            {
                swhere = swhere ?? string.Empty;
                int startindex = (page - 1) * limit + 1;
                int endindex = page * limit;
                if (!string.IsNullOrWhiteSpace(swhere))
                {
                    Dictionary<string, string> jsonObject = JsonConvert.DeserializeObject<Dictionary<string, string>>(swhere);
                    swhere = " 1=1 ";
                    foreach (var item in jsonObject)
                    {
                        if (item.Key == "EPTName")
                        {
                            swhere += $" AND EPTName like '%{item.Value}%' ";
                        }
                    }
                }
                DataSet ds = new BLL.ExamPaperType().GetListByPage(swhere, "EPTID", startindex, endindex);
                resultMsg.Data = ds.Tables[0];
                resultMsg.Count = Convert.ToInt32(ds.Tables[1].Rows[0][0]);
                resultMsg.Info = resultMsg.Count > 0 ? StatusCodeEnum.Success.GetEnumText() : "未获取到相应数据";
            }
            catch (Exception ex)
            {
                resultMsg.StatusCode = (int)StatusCodeEnum.Error;
                resultMsg.Info = StatusCodeEnum.Error.GetEnumText();
                resultMsg.Data = ex.Message;
                log.Error(ex);
            }
            return Json(resultMsg);
        }

        /// <summary>
        /// 试卷类型编辑
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [HttpPost]
        [Route("api/ExamPaperType/Edit")]
        public IHttpActionResult Edit(Model.ExamPaperType model)
        {
            log.Info(JsonConvert.SerializeObject(model));

            ResultMsg resultMsg = new ResultMsg();
            resultMsg.StatusCode = (int)StatusCodeEnum.Success;
            try
            {
                BLL.ExamPaperType _ExamPaperType = new BLL.ExamPaperType();
                var oldModel = _ExamPaperType.GetModel(model.EPTID);
                if (!_ExamPaperType.Exists(model.EPTName)|| oldModel.EPTName== model.EPTName)
                {
                    if (new BLL.ExamPaperType().Update(model))
                    {
                        resultMsg.Info = "编辑成功";
                        resultMsg.Count = 1;
                    }
                    else
                    {
                        resultMsg.Info = "编辑失败";
                    }
                }
                else
                {
                    resultMsg.Info = "类型名称已存在";
                }
            }
            catch (Exception ex)
            {
                resultMsg.StatusCode = (int)StatusCodeEnum.Error;
                resultMsg.Info = StatusCodeEnum.Error.GetEnumText();
                resultMsg.Data = ex.Message;
                log.Error(ex);
            }
            return Json(resultMsg);
        }

        /// <summary>
        /// 试卷类型删除
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [HttpGet]
        [Route("api/ExamPaperType/DelList")]
        public IHttpActionResult DelList(string delidlist)
        {
            ResultMsg resultMsg = new ResultMsg();
            resultMsg.StatusCode = (int)StatusCodeEnum.Success;
            try
            {
                if (new IPR_StudyMeetingMrgSys.BLL.ExamPaperType().DeleteList(delidlist))
                {
                    resultMsg.Info = StatusCodeEnum.Success.GetEnumText();
                    resultMsg.Count = 1;
                }
                else
                {
                    resultMsg.Info = "删除失败";
                }
            }
            catch (Exception ex)
            {
                resultMsg.StatusCode = (int)StatusCodeEnum.Error;
                resultMsg.Info = StatusCodeEnum.Error.GetEnumText();
                resultMsg.Data = ex.Message;
                log.Error(ex);
            }
            return Json(resultMsg);
        }
    }
}

